﻿namespace CKFinder
{
    using System;
    using System.ComponentModel;
    using System.Configuration;
    using System.Reflection;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:FileBrowser runat=server></{0}:FileBrowser>"), Designer(typeof(FileBrowserDesigner))]
    public class FileBrowser : Control
    {
        private const string CKFINDER_DEFAULT_BASEPATH = "/ckfinder/";

        private string BuildCKFinderDirUrl()
        {
            string basePath = this.BasePath;
            if ((basePath == null) || (basePath.Length == 0))
            {
                basePath = "/ckfinder/";
            }
            if (!basePath.EndsWith("/"))
            {
                basePath = basePath + "/";
            }
            return basePath;
        }

        private string BuildUrl()
        {
            string str = this.BuildCKFinderDirUrl();
            string str2 = "";
            str = str + "ckfinder.html";
            if ((this.SelectFunction != null) && (this.SelectFunction.Length > 0))
            {
                str2 = str2 + "?action=js&amp;func=" + this.SelectFunction;
            }
            if (!this.DisableThumbnailSelection)
            {
                if ((this.SelectThumbnailFunction != null) && (this.SelectThumbnailFunction.Length > 0))
                {
                    str2 = (str2 + ((str2.Length > 0) ? "&amp;" : "?")) + "thumbFunc=" + this.SelectThumbnailFunction;
                }
                else if ((this.SelectFunction != null) && (this.SelectFunction.Length > 0))
                {
                    str2 = str2 + "&amp;thumbFunc=" + this.SelectFunction;
                }
            }
            else
            {
                str2 = str2 + ((str2.Length > 0) ? "&amp;" : "?") + "dts=1";
            }
            if ((this.StartupPath != null) && (this.StartupPath.Length > 0))
            {
                str2 = (str2 + ((str2.Length > 0) ? "&amp;" : "?")) + "start=" + HttpContext.Current.Server.UrlPathEncode(this.StartupPath + (this.StartupFolderExpanded ? ":1" : ":0"));
            }
            if ((this.ResourceType != null) && (this.ResourceType.Length > 0))
            {
                str2 = (str2 + ((str2.Length > 0) ? "&amp;" : "?")) + "type=" + HttpContext.Current.Server.UrlPathEncode(this.ResourceType);
            }
            if (!this.RememberLastFolder)
            {
                str2 = str2 + ((str2.Length > 0) ? "&amp;" : "?") + "rlf=0";
            }
            if ((this.CKFinderId != null) && (this.CKFinderId.Length > 0))
            {
                str2 = (str2 + ((str2.Length > 0) ? "&amp;" : "?")) + "id=" + HttpContext.Current.Server.UrlPathEncode(this.CKFinderId);
            }
            return (str + str2);
        }

        public string CreateHtml()
        {
            string className = this.ClassName;
            string cKFinderId = this.CKFinderId;
            if ((className != null) && (className.Length > 0))
            {
                className = " class=\"" + className + "\"";
            }
            if ((cKFinderId != null) && (cKFinderId.Length > 0))
            {
                cKFinderId = " id=\"" + cKFinderId + "\"";
            }
            return string.Concat(new object[] { "<iframe src=\"", this.BuildUrl(), "\" width=\"", this.Width, "\" height=\"", this.Height, "\"", className, cKFinderId, " frameborder=\"0\" scrolling=\"no\"></iframe>" });
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.CreateHtml());
        }

        public void SetupFCKeditor(object fckeditorInstance)
        {
            string basePath = this.BasePath;
            if (!this.BasePath.StartsWith("/"))
            {
                string absolutePath = HttpContext.Current.Request.Url.AbsolutePath;
                this.BasePath = absolutePath.Substring(0, absolutePath.LastIndexOf("/") + 1) + this.BasePath;
            }
            string str2 = this.BuildCKFinderDirUrl() + "core/connector/aspx/connector.aspx?command=QuickUpload";
            string str3 = this.BuildUrl();
            string str4 = (str3.IndexOf("?") == -1) ? "?" : "&amp;";
            try
            {
                object obj2 = fckeditorInstance.GetType().GetProperty("Config").GetValue(fckeditorInstance, null);
                PropertyInfo property = obj2.GetType().GetProperty(((DefaultMemberAttribute) obj2.GetType().GetCustomAttributes(typeof(DefaultMemberAttribute), true)[0]).MemberName);
                property.SetValue(obj2, str3, new object[] { "LinkBrowserURL" });
                property.SetValue(obj2, str3 + str4 + "type=Images", new object[] { "ImageBrowserURL" });
                property.SetValue(obj2, str3 + str4 + "type=Flash", new object[] { "FlashBrowserURL" });
                property.SetValue(obj2, str2, new object[] { "LinkUploadURL" });
                property.SetValue(obj2, str2 + "&type=Images", new object[] { "ImageUploadURL" });
                property.SetValue(obj2, str2 + "&type=Flash", new object[] { "FlashUploadURL" });
            }
            catch
            {
                throw new ApplicationException("SetupFCKeditor expects an FCKeditor instance object.");
            }
        }

        [DefaultValue("/ckfinder/")]
        public string BasePath
        {
            get
            {
                object obj2 = this.ViewState["BasePath"];
                if (obj2 == null)
                {
                    obj2 = ConfigurationSettings.AppSettings["CKFinder:BasePath"];
                }
                if (obj2 != null)
                {
                    return (string) obj2;
                }
                return "/ckfinder/";
            }
            set
            {
                this.ViewState["BasePath"] = value;
            }
        }

        public string CKFinderId
        {
            get
            {
                return (this.ViewState["CKFinderId"] as string);
            }
            set
            {
                this.ViewState["CKFinderId"] = value;
            }
        }

        public string ClassName
        {
            get
            {
                return (this.ViewState["ClassName"] as string);
            }
            set
            {
                this.ViewState["ClassName"] = value;
            }
        }

        public bool DisableThumbnailSelection
        {
            get
            {
                return ((this.ViewState["DisableThumbnailSelection"] != null) && ((bool) this.ViewState["DisableThumbnailSelection"]));
            }
            set
            {
                this.ViewState["DisableThumbnailSelection"] = value;
            }
        }

        [Category("Appearence"), DefaultValue("400px")]
        public Unit Height
        {
            get
            {
                object obj2 = this.ViewState["Height"];
                if (obj2 != null)
                {
                    return (Unit) obj2;
                }
                return Unit.Pixel(400);
            }
            set
            {
                this.ViewState["Height"] = value;
            }
        }

        public bool RememberLastFolder
        {
            get
            {
                if (this.ViewState["RememberLastFolder"] != null)
                {
                    return (bool) this.ViewState["RememberLastFolder"];
                }
                return true;
            }
            set
            {
                this.ViewState["RememberLastFolder"] = value;
            }
        }

        public string ResourceType
        {
            get
            {
                return (this.ViewState["ResourceType"] as string);
            }
            set
            {
                this.ViewState["ResourceType"] = value;
            }
        }

        public string SelectFunction
        {
            get
            {
                return (this.ViewState["SelectFunction"] as string);
            }
            set
            {
                this.ViewState["SelectFunction"] = value;
            }
        }

        public string SelectThumbnailFunction
        {
            get
            {
                return (this.ViewState["SelectThumbnailFunction"] as string);
            }
            set
            {
                this.ViewState["SelectThumbnailFunction"] = value;
            }
        }

        public bool StartupFolderExpanded
        {
            get
            {
                return ((this.ViewState["StartupFolderExpanded"] != null) && ((bool) this.ViewState["StartupFolderExpanded"]));
            }
            set
            {
                this.ViewState["StartupFolderExpanded"] = value;
            }
        }

        public string StartupPath
        {
            get
            {
                return (this.ViewState["StartupPath"] as string);
            }
            set
            {
                this.ViewState["StartupPath"] = value;
            }
        }

        [Category("Appearence"), DefaultValue("100%")]
        public Unit Width
        {
            get
            {
                object obj2 = this.ViewState["Width"];
                if (obj2 != null)
                {
                    return (Unit) obj2;
                }
                return Unit.Percentage(100.0);
            }
            set
            {
                this.ViewState["Width"] = value;
            }
        }
    }
}

